package com.itheima.project.log.impl;

import com.itheima.project.log.LogInfoService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * <b>ClassName：LogInfoServiceImpl
 * @Description：日志实现
 */
@Component("logInfoService")
@Aspect
public class LogInfoServiceImpl implements LogInfoService {

    private static Logger log = LogManager.getLogger(LogInfoServiceImpl.class);

    @Override
    @Before("execution(* com.itheima.project.service.impl.*.*(..))")
    public void beforeLogInfo(JoinPoint joinPoint) {
//        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
//        Method method = methodSignature.getMethod();
//        service.info("前置通知内容："+method.getName());
    }

    @Override
    @AfterReturning("execution(* com.itheima.project.service.impl.*.*(..))")
    public void afterReturningLogInfo(JoinPoint joinPoint) {
//        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
//        Method method = methodSignature.getMethod();
//        service.info("后置正常返回通知内容："+method.getName());
    }

    @Override
    @AfterThrowing("execution(* com.itheima.project.service.impl.*.*(..))")
    public void afterThrowingLogInfo(JoinPoint joinPoint) {
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        Method method = methodSignature.getMethod();
        log.info("后置通异常知内容："+method.getName());
    }

    @Override
    @After("execution(* com.itheima.project.service.impl.*.*(..))")
    public void afterLogInfo(JoinPoint joinPoint) {
//        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
//        Method method = methodSignature.getMethod();
//        service.info("最终通知内容："+method.getName());
    }

    @Override
    @Around("execution(* com.itheima.project.service.impl.*.*(..))")
    public Object aroundLogInfo(ProceedingJoinPoint joinPoint) {
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        Method method = methodSignature.getMethod();
        Object result = null;
        try {
            log.info("环绕-前置通知："+method.getName());
            result= joinPoint.proceed();
            log.info("环绕-后置正常通知："+method.getName());
        } catch (Throwable throwable) {
            log.info("环绕-后置异常通知："+method.getName());
            throwable.printStackTrace();
        }finally {
            log.info("环绕-最终通知："+method.getName());
        }
        return result;
    }
}
